package com.example.project.projectspringcloud.controller.admin.sysconfig;

import com.example.project.projectspringcloud.common.Constants;
import com.example.project.projectspringcloud.common.OperationException;
import com.example.project.projectspringcloud.entity.RespBody;
import com.example.project.projectspringcloud.entity.sys.SysConfig;
import com.example.project.projectspringcloud.service.sys.SysConfigService;
import com.example.project.projectspringcloud.utils.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 系统设置Controller
 */
@Api(tags = "系统设置控制器")
@Controller
@RequestMapping(value = "/admin/sysconfig")
public class AdminSysConfigController {

    @Autowired
    private SysConfigService sysConfigService;

    /**
     * 返回系统设置列表
     * ==============================================================================
     *
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "系统设置列表", notes = "返回系统设置列表")
    @GetMapping(value = {"", "/list"})
    public String list(SysConfig sysConfig, Model model) throws Exception {
        List<SysConfig> sysConfigList = sysConfigService.list(null, sysConfig);
        model.addAttribute("sysConfigList", sysConfigList);
        model.addAttribute("sysConfig", sysConfig);
        return "admin/sysconfig/sysconfig-list";
    }

    /**
     * 删除
     * ==============================================================================
     *
     * @param ids
     * @return
     */
    @ApiOperation(value = "删除系统配置数据", notes = "根据ID删除系统配置数据")
    @DeleteMapping(value = "/delete/{ids}")
    @ResponseBody
    public RespBody delete(@ApiParam(value = "id/ids 1,2,3...") @PathVariable("ids") String ids) {
        if (StringUtils.isBlank(ids)) {
            return RespBody.error(Constants.Message.PARAM_INVALID);
        }
        Integer[] idarr = StringUtil.split(ids, ",");
        if (!sysConfigService.delete(idarr)) {
            return RespBody.error(Constants.Message.DELETE_FAILED);
        }
        return RespBody.ok(Constants.Message.DELETE_SUCCESS);
    }

    /**
     * 获取表单
     * ==============================================================================
     *
     * @param id
     * @param model
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "获取表单", notes = "获取表单信息")
    @GetMapping(value = "/form")
    public String form(@RequestParam(value = "id", required = false) Integer id, Model model) throws Exception {
        if (id != null) {
            SysConfig sysConfig = sysConfigService.get(new SysConfig(id));
            model.addAttribute("sysConfig", sysConfig);
        }
        return "admin/sysconfig/sysconfig-form";
    }

    /**
     * 保存系统配置信息
     * ==============================================================================
     *
     * @param sysConfig
     * @return
     */
    @ApiOperation(value = "API保存系统配置", notes = "根据ID判断更新/插入")
    @PostMapping(value = "/save")
    @ResponseBody
    public RespBody save(SysConfig sysConfig) throws Exception {
        try {
            if (StringUtils.isNotBlank(sysConfig.getPlainText())) {
                sysConfig.setValue(StringUtil.replaceHtml(sysConfig.getValue()));
            }
            boolean result = sysConfigService.save(sysConfig);
            if (!result) {
                return RespBody.error(Constants.Message.SAVE_FAILED);
            }
        } catch (OperationException e) {
            return RespBody.error(e.getMessage());
        }

        return RespBody.ok(Constants.Message.SAVE_SUCCESS);
    }

    /**
     * api获取配置
     * ==============================================================================
     *
     * @param sysConfig
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "API获取系统配置", notes = "获取系统配置")
    @GetMapping(value = "/api/get")
    @ResponseBody
    public RespBody get(SysConfig sysConfig) throws Exception {
        sysConfig = sysConfigService.get(sysConfig);
        return RespBody.ok(sysConfig);
    }
}
